#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

def nutrition_stat(input_path: str, output_path: str):
    stat = {}
    infile = open(input_path, "r")
    infile.readline()
    for line in infile:
        line = line.rstrip("\n")
        splitline = line.split("\t")
        nutrition_list = splitline[4].split("|")
        rank = splitline[6]

        for nutrition in nutrition_list:
            if nutrition not in stat:
                stat[nutrition] = {"total": 0, "rank9": 0, "rank7": 0, "rank5": 0, "rank3": 0, "rank1": 0}
            stat[nutrition]["total"] += 1
            stat[nutrition]["rank" + str(rank)] += 1
    infile.close()

    outfile = open(output_path, "w")
    outfile.write(
        "%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ("nutrition", "total", "rank 9", "rank 7", "rank 5", "rank 3", "rank 1"))
    outfile.flush()
    for nutrition in stat:
        nutrition_dict = stat[nutrition]
        outfile.write("%s\t%d\t%d\t%d\t%d\t%d\t%d\n" % (
            nutrition, nutrition_dict["total"], nutrition_dict["rank9"], nutrition_dict["rank7"],
            nutrition_dict["rank5"], nutrition_dict["rank3"], nutrition_dict["rank1"]))
        outfile.flush()
    outfile.close()


if __name__ == '__main__':
    nutrition_stat("/Users/genesis/Seafile/Work/营养知识图谱/integrated_list_valid_array.tsv",
                   "/Users/genesis/Seafile/Work/营养知识图谱/nutrition_stat.tsv")
